<!DOCTYPE html>
<html lang="zh-cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>低代码应用服务平台</title>
</head>
<body>
<div id="errormsg" style="display: none"></div>
</body>
<script th:src="@{/vendor/jquery/jquery.min.js}"></script>
<script  th:inline="javascript">
    (function (window, document, $) {
        'use strict';
        var loc =  location.protocol + '//' + location.host;
        var redirectUri = "workbench-center/workbench";
        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
        $.errorHandle = function (errmsg){
            $("#errormsg").text(errmsg);
            $("#errormsg").css("display","block");
            setTimeout(()=>{
                window.location.href=loc+"/login";
            },5000);
        }
        $.loginsy = function (idToken) {
            $("#errormsg").css("display","none");
            $.ajax({
                url: loc + "/api/login/idaas/sso",
                type: 'GET',
                data: {id_token:idToken},
                async: false,
                success: function (result) {
                    debugger;
                    if (result && result.errcode == 0) {
                        const isAdmin = result.userinfo.username == 'admin';
                        localStorage.setItem('refresh_token', result.refresh_token);
                        localStorage.setItem('expireTime', result.expiration);
                        localStorage.setItem('token', result.access_token);
                        localStorage.setItem('corpId',result.userinfo?result.userinfo.corpId:"main");
                        localStorage.setItem('_isAdmin',isAdmin + '');
                        window.location.href=loc+"/"+redirectUri;
                    }else{
                        $.errorHandle(result.errmsg);
                        //
                    }
                },
                complete:function (xhr) {
                    if("REDIRECT" == xhr.getResponseHeader("REDIRECT")){ //若HEADER中含有REDIRECT说明后端想重定向，
                        location.href = xhr.getResponseHeader("CONTENTPATH");//将后端重定向的地址取出来,使用location.href去实现重定向的要求
                    }
                },
                error: function (event,xhr,error) {
                    $.errorHandle(error);
                }
            })
        }

        //let token = localStorage.getItem("token");
        let token = "";
        let idToken = [[${id_token}]];

        //如果token为空,!token结果为true
        if(!token){
            $.loginsy(idToken);
        }else{
            window.location.href=loc+"/"+targetUrl;
        }
    })(window, document, jQuery);

</script>
</html>